using Abc.Core;
using Fur;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utils;

namespace Abc.Application
{
    [Injection(Proxy = typeof(LogDispatchProxy))]
    public class RoleServices : IRoleServices, ITransient
    {
        private readonly IRepository<Role> _roleRepository;
        public RoleServices(IRepository<Role> roleRepository)
        {
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 新增一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> Insert(RoleDto input)
        {
            var resultdata = new ResultData<string>();
            // 如果不需要返回自增Id，使用 InsertAsync即可
            var newEntity = await _roleRepository.InsertAsync(input.Adapt<Role>());

          

            resultdata.State = true;
            resultdata.Msg = "成功";
            return resultdata;
        }


        /// <summary>
        /// 更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> Update(RoleDto input)
        {
            var resultdata = new ResultData<string>();
            var dbentity = await _roleRepository.Entities.SingleAsync(u => u.Id == input.Id);
            input.Adapt(dbentity);
            await _roleRepository.UpdateAsync(dbentity);

            

            resultdata.State = true;
            resultdata.Msg = "成功";
            return resultdata;
        }

        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> AddOrUpdate(RoleDto input)
        {
            if (input.Id > 0)
            {
                return await Update(input);
            }
            else
            {
                return await Insert(input);
            }
        }


        public async Task<ResultData<string>> RoleSetting(int roleid, List<int> menuids)
        {
            var resultdata = new ResultData<string>();

            var dbentity = await _roleRepository.Entities.Include(o => o.Securities).SingleAsync(u => u.Id == roleid);

            foreach (var menuid in dbentity.Securities.Select(o => o.Id).ToList())
            {
                if (!menuids.Any(a => a == menuid))
                {
                    dbentity.Securities.Remove(dbentity.Securities.First(o => o.Id == menuid));
                }
            }

            var _menuRepository = _roleRepository.Change<Security,MasterDbContextLocator>() ;
            var menus = await _menuRepository.Entities.ToListAsync() ;

            foreach (var menuid in menuids)
            {
                if (dbentity.Securities.Any(o => o.Id == menuid)) continue;
                dbentity.Securities.Add(menus.First(o=>o.Id==menuid));
            }


            await _roleRepository.SaveNowAsync();

          

            resultdata.State = true;
            resultdata.Msg = "成功";
            return resultdata;
        }


        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        public async Task<ResultData<string>> Delete(int id)
        {
            var resultdata = new ResultData<string>();
            await _roleRepository.DeleteAsync(id);

           

            resultdata.State = true;
            resultdata.Msg = "成功";
            return resultdata;
        }

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        public async Task<RoleDto> Get(int id)
        {
            var dbentity =  _roleRepository.Include(o=>o.Securities).FirstOrDefault(o=>o.Id==id);
            return dbentity.Adapt<RoleDto>();
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleDto>> GetAll()
        {
            var entities = _roleRepository.AsQueryable().ProjectToType<RoleDto>();
            return await entities.ToListAsync();
        }


        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="querydto"></param>
        /// <returns></returns>
        public async Task<Fur.DatabaseAccessor.PagedList<RoleDto>> GetAllByPage(RoleQueryDto querydto)
        {
            var pageResult = _roleRepository.AsQueryable().ProjectToType<RoleDto>();
            return await pageResult.ToPagedListAsync(querydto.Page, querydto.Limit);
        }
      
    }


    public interface IRoleServices
    {
        /// <summary>
        /// 新增一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<ResultData<string>> Insert(RoleDto input);

        /// <summary>
        /// 更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<ResultData<string>> Update(RoleDto input);


        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<ResultData<string>> AddOrUpdate(RoleDto input);

        Task<ResultData<string>> RoleSetting(int roleid, List<int> menuids);
        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        Task<ResultData<string>> Delete(int id);

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        Task<RoleDto> Get(int id);

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        Task<List<RoleDto>> GetAll();


        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="querydto"></param>
        ///// <returns></returns>
        //[NonTransact]
        //Task<PagedListToLayui<RoleDto>> GetAllByPageToLayui(RoleQueryDto querydto);
        ///// <summary>
        ///// 更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Update(List<RoleDto> inputs);
        ///// <summary>
        ///// 新增多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Insert(List<RoleDto> inputs);
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> AddOrUpdate(List<RoleDto> inputs);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="querydto"></param>
        /// <returns></returns>
        Task<Fur.DatabaseAccessor.PagedList<RoleDto>> GetAllByPage(RoleQueryDto querydto);
    }



}
